package com.chenshu.shop.common;

import com.chenshu.shop.model.Admin;
import com.chenshu.shop.model.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA
 * Description：获取会话信息的工具
 * User：chenshu
 * Date：2024-06-04
 * Time：16:38
 */
public class SessionUtil {
    //获取Admin的登陆信息
    public static Admin getLoginAdmin(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        if (session != null && session.getAttribute(Constant.SESSION_ADMIN_INFO_KEY)!= null) {
            return (Admin) session.getAttribute(Constant.SESSION_ADMIN_INFO_KEY);
        }
        return null;
    }

    //获取User的登录信息
    public static User getLoginUser(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        if (session != null && session.getAttribute(Constant.SESSION_USER_INFO_KEY)!= null) {
            return (User) session.getAttribute(Constant.SESSION_USER_INFO_KEY);
        }
        return null;
    }

    //获取User的购物车数量
    public static Integer getCartCount(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        if (session != null && session.getAttribute(Constant.USER_CART_COUNT)!= null) {
            return (Integer) session.getAttribute(Constant.USER_CART_COUNT);
        }
        return null;
    }
}
